layui.use(['form', 'layer', 'upload'], function () {
    const form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        $ = layui.jquery;


    let doctorId = $.cookie("doctorId");
    if (JSON.parse(sessionStorage.getItem('memberData')) != null) {
        let obj = JSON.parse(sessionStorage.getItem('obj'));
        doctorId = obj.doctorId;
        serverIP = obj.serverIP;
        serverApi = obj.serverApi;
        serverApi3 = obj.serverApi3;
        serverApi1 = obj.serverApi1;
        socketUrl6 = obj.socketUrl6;
    }

    //多图片上传
    let coverUrl = "";
    upload.render({
        elem: '#test2'
        , url: host + '/a03/umsmember/uploadImage'
        , method: 'post'  //可选项。HTTP类型，默认post
        , field: 'multipartFile'
        , multiple: true
        , number: 5
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo2').append('<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img thumbImg" style="margin-left: 2px">')
            });
        }
        , done: function (res) {
            //上传完毕
            //如果上传失败
            if (res.code !== 200) {
                return layer.msg('上传失败');
            } else {
                //上传成功
                coverUrl += res.data + ",";
            }
        }
    });

    //点击重置
    $("body").on("click", "#reset2", function () {
        window.location.reload()
    });

    //点击确定
    form.on("submit(formDemo)", function (data) {
        let memberData = JSON.parse(sessionStorage.getItem('memberData'));
        if (memberData === null) {
            layer.msg("未接通患者")
            return true;
        }

        layer.confirm('此建议将会发送给患者，是否确认?', {title: '系统提示', area: ['320px', '200px']}, function (index0) {
            //do something
            //弹出loading
            const index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
            var reqData = {
                content: data.field.content,
                doctorId: doctorId,
                memberId: $.cookie("memberId"),
                pic: coverUrl,
                zhenid: memberData.diagnosisId
            };
            $.ajax({
                url: serverApi + "/doctoradvise/find01Add" + "?" + formatParams(reqData),
                type: "POST",
                contentType: "application/x-www-form-urlencoded",
                success: function (result) {
                    if (result.code === 200) {

                        //图文还是视频
                        if (sessionStorage.getItem("changeLeft")) {
                            sessionStorage.setItem("twShowJianYiId", result.data)
                        } else {
                        }

                        parent.layer.closeAll();
                        parent.layer.msg("建议已成功发送给患者");
                        location.reload();
                    } else {
                        layer.msg('请求失败', {icon: 7, anim: 6});
                    }
                }
            });

            layer.close(index);
        });
        return false;
    });
});